<template>
  <div>
    <a-card class="card-header">
      <a-row :gutter="24">
        <a-col :span="3">
          <div class="flex flex-a-c">
            <div style="width:80px">员工：</div>
            <a-input
              v-model.trim="queryParam.name"
              placeholder="请输入用户名称"
            ></a-input>
          </div>
        </a-col>
        <a-col :span="3">
          <a-button type="primary" @click="handleSearch">查询</a-button>
        </a-col>
      </a-row>
    </a-card>

    <a-card class="card-content">
      <a-row :gutter="24">
        <a-col>
          <a-button type="primary" @click="handleAdd">新建</a-button>
          <!-- <a-button type="primary" @click="handleAdd">导出</a-button> -->
          <!-- <a-button type="primary" @click="handleAdd">导入</a-button> -->
        </a-col>
      </a-row>
      <br />
      <a-row :gutter="16">
        <a-col :span="5">
          <div class="treeBox">
            <a-tree
              :show-line="true"
              :tree-data="treeData"
              @select="onSelect"
              defaultExpandAll
              :checkable="false"
              :replaceFields="{
                children: 'childList',
                title: 'departmentName',
                key: 'id',
                value: 'id',
              }"
            >
            </a-tree>
          </div>
        </a-col>
        <a-col :span="19">
          <a-table
            :scroll="{ x: 1500, y: 300 }"
            :columns="columns"
            :data-source="tableData"
            :loading="loading"
            :pagination="pagination"
            @change="handleTableChange"
            bordered
            :rowKey="(record, index) => record.id"
          >
            <template slot="action" slot-scope="text, record">
              <a @click="handleEdit(record)">编辑</a>
              <a-divider type="vertical" />
              <a-popconfirm
                okText="确定"
                cancelText="取消"
                v-if="tableData.length"
                title="确定删除吗?"
                @confirm="() => handleDelete(record)"
              >
                <a>删除</a>
              </a-popconfirm>
            </template>
          </a-table>
        </a-col>
      </a-row>
    </a-card>

    <UserModal
      ref="modalForm"
      :treeData="treeData"
      @ok="modalFormOk"
    ></UserModal>
  </div>
</template>

<script>
import { columns } from "./components/tableConfig";
import { tableMinxins } from "@/unit/mixins/tableMinxins.js";
import UserModal from "./components/UserModal.vue";
import api from "@/api/apiServer";

export default {
  components: { UserModal },
  mixins: [tableMinxins],
  data() {
    return {
      columns,
      treeData: [],
      queryParam: {
        department: "",
      },
    };
  },
  mounted() {
    this.getTree();
    this.getList();
  },
  methods: {
    // 获取列表数据
    async getList(arg) {
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.pagination.current = 1;
      }
      var params = this.getQueryParams();
      const res = await api.UserList(params);
      this.tableData = res.data.data;
      // this.loading = true;
    },
    async getTree() {
      const res = await api.DepartmentTree();
      this.treeData = res.data;
    },
    handleAdd() {
      this.$router.push("/custFormAdd");
    },
    // 删除
    async handleDelete(record) {
      let params = {
        userName: record.userName,
        id: record.id,
      };
      const res = await api.UserDelete(params);
      this.$message.success(res.message);
      this.getList();
    },
    onSelect(selectedKeys, info) {
      this.pagination.current = 1;
      this.pagination.pageNumber = 1;
      this.queryParam.department = selectedKeys.toString();
      this.getList();
    },
  },
};
</script>

<style lang="less" scoped>
.card-header {
  margin: 20px 0;
}
.card-content {
  button {
    margin-right: 13px;
  }
}
</style>
